package com.sui.bigdata.rtcadmin.util;


import com.feidee.fdcommon.configuration.CustomConfiguration;
import com.google.common.collect.Maps;
import com.sui.bigdata.rtcadmin.dto.JobConfigDto;
import com.sui.bigdata.rtcadmin.repository.model.JobConfig;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Component;

import java.util.Map;

/**
 * @author YongChen
 * @date 2019/11/15 16:28
 * @description
 * @email yong_chen@sui.com
 */
@Component
public class BasicPropUtils {

    public static Map<String, String> buildBasicProp() {
        Map<String, String> basicProp = Maps.newHashMap();
        basicProp.put("bootstrapServers", CustomConfiguration.getString("kafka.bootstrap.servers"));
        basicProp.put("url",CustomConfiguration.getString("tidb.url"));
        basicProp.put("userName",CustomConfiguration.getString("tidb.user.name"));
        basicProp.put("password",CustomConfiguration.getString("tidb.password"));
        basicProp.put("zookeeperQuorum",CustomConfiguration.getString("zookeeper.quorum"));
        basicProp.put("kuduMasters",CustomConfiguration.getString("kuduMasters"));
        return basicProp;
    }

    public static void buildRestartCofing(JobConfigDto currentConfig , JobConfig oldConfig ) {

        if (!StringUtils.isNotBlank(currentConfig.getDeveloper())){
            currentConfig.setDeveloper(oldConfig.getDeveloper());
        }
        if (!StringUtils.isNotBlank(currentConfig.getAlarmGroupPhone())){
            currentConfig.setAlarmGroupPhone(oldConfig.getAlarmGroupPhone());
        }

        if (!StringUtils.isNotBlank(currentConfig.getExecSql())) {
            currentConfig.setExecSql(oldConfig.getExecSql());
        }
        if (currentConfig.getParallelism() == 0) {
            currentConfig.setParallelism(oldConfig.getParallelism());
        }

        if (!StringUtils.isNotBlank(currentConfig.getQueue())) {
            currentConfig.setQueue(oldConfig.getQueue());
        }

        if (currentConfig.getJobManagerMemory() == 0) {
            currentConfig.setJobManagerMemory(oldConfig.getJobManagerMemory());
        }
        if (currentConfig.getTaskManagerMemory() == 0) {
            currentConfig.setTaskManagerMemory(oldConfig.getTaskManagerMemory());
        }
        if (currentConfig.getSlots() == 0) {
            currentConfig.setSlots(oldConfig.getSlots());
        }

        if (!StringUtils.isNotBlank(currentConfig.getExtendParam())) {
            currentConfig.setExtendParam(oldConfig.getExtendParam());
        }
        if (StringUtils.isBlank(currentConfig.getDescription())) {
            currentConfig.setDescription(oldConfig.getDescription());
        }
    }
}
